package com.gitee.sop.layui.adminserver.plugins.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录错误信息管理
 * @author: huwei
 * @date: 2019/12/5 11:19
 * @version: 1.0.0
 */
public class LoginExceptionManager {

    /**
     * 错误对象管理器
     */
    private static final Map<String ,Class<? extends Throwable>> exceptionHolder= new HashMap();
    static{
        exceptionHolder.put(AuthenticationException.class.getName() ,AuthenticationException.class);
        exceptionHolder.put(LockedAccountException.class.getName() , LockedAccountException.class);
        exceptionHolder.put(UnknownAccountException.class.getName() ,UnknownAccountException.class);
    }

    public static String errorMsg(Throwable e ){
        return errorMsg(e ,null);
    }

    public static String errorMsg(Throwable e,String defaultMessage){
       Class<?> cause = exceptionHolder.get(e.getClass().getName());
       if(cause != null){
           return e.getMessage();
       }else{
           if(StringUtils.isEmpty(defaultMessage)){
               return "登录异常,请稍后重试";
           }
           return defaultMessage;
       }
    }

}
